package com.huluobo.base.db;

import android.app.Application;

import androidx.room.Room;

/**
 * Created by LC on 2024/10/8.
 */
public class AppDbUtils {
    private static final String DB_NAME = "douyin.db";
    private static AppDbUtils appDbUtils;
    private AppDatabase appDatabase;
    private VideoDao videoDao;
    private MoneyDao moneyDao;
    private UserDao userDao;

    private AppDbUtils() {
    }

    public static AppDbUtils getInstance() {
        if (appDbUtils == null) {
            synchronized (AppDbUtils.class) {
                if (appDbUtils == null) {
                    appDbUtils = new AppDbUtils();
                }
            }
        }
        return appDbUtils;
    }

    public void init(Application application) {
        appDatabase = Room.databaseBuilder(application, AppDatabase.class, DB_NAME)
                .allowMainThreadQueries()
                .build();
        videoDao = appDatabase.getVideoDao();
        moneyDao = appDatabase.getMoneyDao();
        userDao = appDatabase.getUserDao();
    }

    public VideoDao getVideoDao() {
        if (videoDao == null) {
            videoDao = appDatabase.getVideoDao();
        }
        return videoDao;
    }

    public MoneyDao getMoneyDao() {
        if (moneyDao == null) {
            moneyDao = appDatabase.getMoneyDao();
        }
        return moneyDao;
    }

    public UserDao getUserDao() {
        if (userDao == null) {
            userDao = appDatabase.getUserDao();
        }
        return userDao;
    }
}
